package com.cq.hd.member.service.impl;

import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.common.utils.GenerateUtil;
import com.cq.hd.common.utils.NoUtils;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.dto.FeedbackPageDto;
import com.cq.hd.member.api.dto.FeedbackSaveDto;
import com.cq.hd.member.api.vo.FeedbackPageVo;
import com.cq.hd.member.po.TbFeedbackPo;
import com.cq.hd.member.mapper.TbFeedbackMapper;
import com.cq.hd.member.service.TbFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 反馈建议表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-19
 */
@Service
public class TbFeedbackServiceImpl extends ServiceImpl<TbFeedbackMapper, TbFeedbackPo> implements TbFeedbackService {

    @Override
    public FeedbackPageVo details(Long id) {
        TbFeedbackPo tbFeedbackPo = baseMapper.selectById(id);
        if (tbFeedbackPo != null && tbFeedbackPo.getDelState() == 0) {
            FeedbackPageVo feedbackPageVo = new FeedbackPageVo();
            BeanUtils.copyProperties(tbFeedbackPo, feedbackPageVo);
            return feedbackPageVo;
        }
        return null;
    }

    @Override
    public Page<FeedbackPageVo> pageFeedback(FeedbackPageDto feedbackPageDto) {
        String startTime = feedbackPageDto.getStartTime();
        String endTime = feedbackPageDto.getEndTime();
        if (!StringUtils.isBlank(startTime) && startTime.length() != 19) {
            Throw.isBusinessException("日期格式错误");
        }
        if (!StringUtils.isBlank(endTime) && endTime.length() != 19) {
            Throw.isBusinessException("日期格式错误");
        }

        PageHelper.startPage(feedbackPageDto.getPageNum(), feedbackPageDto.getPageSize());
        List<FeedbackPageVo> feedbackPageVoList = baseMapper.listFeedbackByDto(feedbackPageDto);
        PageInfo<FeedbackPageVo> pageInfo = new PageInfo<>(feedbackPageVoList);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    @Override
    public Boolean saveFeedback(FeedbackSaveDto feedbackSaveDto) {
        TbFeedbackPo tbFeedbackPo = new TbFeedbackPo();
        BeanUtils.copyProperties(feedbackSaveDto, tbFeedbackPo);
        tbFeedbackPo.setFeedbackNo(NoUtils.getFeedBackNo());
        tbFeedbackPo.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(tbFeedbackPo);
        if (insert == 0) {
            Throw.isBusinessException("提交反馈失败");
        }

        return true;
    }
}
